package GUI;

import java.awt.*;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import javax.swing.*;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Bùi Hữu Hiệp
 */
public class AutoSuggest extends JPanel{
    private final JTextField _text;
    private final JComboBox _comboBox = new JComboBox();
    private final Vector<String> v = new Vector<>();
    
    //Ham khoi tao
    // Mang arr
    // Kich thuoc (sizeX, sizeY)
    // Toa do (x, y)
    public AutoSuggest(ResultSet a, int column, int sizeX, int sizeY, int x, int y) throws SQLException 
    {
        super(new BorderLayout());
        
        this.setSize(sizeX, sizeY);
        this.setLocation(x, y);
        
        _comboBox.setEditable(true);
        _text = (JTextField) _comboBox.getEditor().getEditorComponent();
        _text.addKeyListener(new KeyAdapter() 
        {
            @Override
            public void keyTyped(KeyEvent e) 
            {
                EventQueue.invokeLater(new Runnable() 
                {
                    @Override
                    public void run() 
                    {
                        String text = _text.getText();
                        if(text.length()==0) 
                        {
                            _comboBox.hidePopup();
                            setModel(new DefaultComboBoxModel(v), "");
                        }
                        else
                        {
                            DefaultComboBoxModel m = getSuggestedModel(v, text);
                            if(m.getSize()==0 || hide_flag) 
                            {
                                _comboBox.hidePopup();
                                hide_flag = false;
                            }
                            else
                            {
                                setModel(m, text);
                                _comboBox.showPopup();
                            }
                        }
                    }
                });
            }
            
            @Override
            public void keyPressed(KeyEvent e) 
            {
                String text = _text.getText();
                int code = e.getKeyCode();
                if(code==KeyEvent.VK_ENTER) 
                {
                    if(!v.contains(text)) 
                    {
                        v.addElement(text);
                        Collections.sort(v);
                        setModel(getSuggestedModel(v, text), text);
                    }
                    hide_flag = true; 
                }
                else
                {
                    if(code==KeyEvent.VK_ESCAPE) 
                    {
                        hide_flag = true; 
                    }
                    else if(code==KeyEvent.VK_RIGHT) 
                    {
                        for(int i=0;i<v.size();i++) 
                        {
                            String str = v.elementAt(i);
                            if(str.startsWith(text)) 
                            {
                                _comboBox.setSelectedIndex(0);
                                _text.setText(str);
                                return;
                            }
                        }
                    }
                }
            }
      });
  
        while(a.next())
        {
            v.addElement((String) a.getObject(column));
        }
        
        setModel(new DefaultComboBoxModel(v), "");
        add(_comboBox, BorderLayout.NORTH);
       
    }
    
    private boolean hide_flag = false;
    
    private void setModel(DefaultComboBoxModel mdl, String str) 
    {
     _comboBox.setModel(mdl);
     _comboBox.setSelectedIndex(0);
     
     _text.setText(str);
    }
            
    private static DefaultComboBoxModel getSuggestedModel(java.util.List<String> list, String text) 
    {
        DefaultComboBoxModel m = new DefaultComboBoxModel();
        for(String s: list) 
        {
            if(s.startsWith(text)) m.addElement(s);
        }
        return m;
    }
    
    public Object GetSelectItem()
    {
        return _comboBox.getSelectedItem();
    }
    
    public void SetSelectItem(Object item)
    {
        _comboBox.setSelectedItem(item);
    }
    
    public JComboBox getComboBox()
    {
        
        return _comboBox;
    }
}
